package org.wanda.pv.service.business.impl;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.business.Mark;
import org.wanda.pv.model.business.MarkFileRe;
import org.wanda.pv.model.business.Topic;
import org.wanda.pv.service.BaseServiceImpl;
import org.wanda.pv.service.business.MarkService;
import org.wanda.pv.service.file.FileUploadService;

import java.util.List;

/**
 * @author lxy
 */

@Service
public class MarkServiceImpl extends BaseServiceImpl<Mark> implements  MarkService{
    @Autowired
    public MarkServiceImpl(Dao dao) {
        super(dao);
    }

    @Autowired
    FileUploadService fileUploadService;

    @Override
    public Result updateMarkStatus(Long id,boolean passed) {
        try{
            Mark mark=dao().fetch(Mark.class,Cnd.where("id","=",id).and("delFlag", "=", "0"));
            if(mark !=null){
                mark.setPassed(passed);
                dao().update(mark);
                return   Result.success("success","标注审核状态修改成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error("标注不存在！");
    }

    @Override
    public Result del(Long id){
        Mark mark=dao().fetch(Mark.class, id);
        if(mark !=null){
            /*
            * 删除标注和要点表
            * 标注和回复表
            * 标注和图纸表
            * */
            Sql sql=Sqls.create("DELETE from wd_re_mark_point where mark_id = @markId");
            sql.setParam("markId",mark.getId());
            Sql sql1=Sqls.create("DELETE from wd_re_mark_topic where mark_id = @markId");
            Sql sql2=Sqls.create("DELETE from wd_re_mark_hy_drawing where mark_id = @markId");
            sql1.setParam("markId",mark.getId());
            sql2.setParam("markId",mark.getId());
            dao().execute(sql,sql1,sql2);
            List<Topic> list =dao().query(Topic.class,Cnd.where("mark_id","=",mark.getId()).and("delFlag", "=", "0"));
            list.forEach(topic -> {
                dao().delete(topic);
            });
            dao().delete(mark);
            return   Result.success("success","标注删除成功！");
        }
            return Result.error("标注不存在！");
    }

    @Override
    public Mark create(Long drawingId, String description, String site, String imgStr, Long pointId, Long userId,String projectFlowChildrenNodeId,boolean passed)throws Exception {
        Mark mark = new Mark();
            if (Strings.isNotBlank(imgStr)){
                Long fileId = fileUploadService.base64ToJpg(imgStr);
                mark.setThumbnail(fileId);
            }
            mark.setPassed(passed);
            mark.setDescription(description);
            mark.setDrawingId(drawingId);
            mark.setPointId(pointId);
            mark.setProjectFlowChildrenNodeId(projectFlowChildrenNodeId);
            mark.setSite(site);
            mark.setHyUserInfoId(userId);
            mark.setIsOptimization(-1);
            mark = this.insert(mark);
            /** 设置要点与标注的关联关系**/
            if(pointId!=null){
                dao().insert("wd_re_mark_point", Chain.make("mark_id",mark.getId()).add("point_id",pointId));
            }
        /**
         * 添加标注和图纸的关联关系
         */
        if(drawingId !=null){
                MarkFileRe markFileRe=new MarkFileRe();
                markFileRe.setMarkId(mark.getId());
                markFileRe.setDrawingId(drawingId);
                markFileRe.setCurrentVersion(true);
                dao().insert(markFileRe);
        }
        return mark;
    }
}
